package com.hf.reflectiondemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestDriver {
    public static void main(String[] args) {
        Test01();
        System.out.println("=================================");
        Test02();
        System.out.println("=================================");
        Test03();
        System.out.println("=================================");
        Test04();
    }


    /**
     * 反射工具类-Constructor工具类
     */
    public static void Test04() {
        //(3)实例对象。getClass()方法
        Student student = new Student();
        //继承Object类方法
        Class clazz2 = student.getClass();

        //获取一个指定名字的方法对象
        try {
            //无参方法
            Method method = clazz2.getDeclaredMethod("doStuding",String.class);
            // 使用invoke调用method对象，让该方法对象动态执行起来
            //有参方法
            method.invoke(student, "tjise");
            //调用无参方法
            //student.doStuding();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    /**
     * 反射工具类-Constructor工具类
     */
    public static void Test03() {
        //(3)实例对象。getClass()方法
        Student student = new Student();
        //继承Object类方法
        Class clazz2 = student.getClass();

        try {
            //获取一个指定名字的属性对象
            Field field = clazz2.getDeclaredField("stuName");
            //设置属性对象课以操作私有对象
            field.setAccessible(true);
            // 调用属性对象的set方法，给属性对象进行设值；需要绑定该属性来自的实例对象
            field.set(student, "zhangsan");
            Field field1 = clazz2.getDeclaredField("stuNum");
            //设置属性对象课以操作私有对象
            field1.setAccessible(true);
            // 调用属性对象的set方法，给属性对象进行设值；需要绑定该属性来自的实例对象
            field1.set(student, "20120");
            System.out.println(student);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * 反射工具类-Constructor工具类
     */
    public static void Test02() {
        //（1）类名.class
        Class<Student> clazz = Student.class;


        try {
            //获取类的构造方法对象
            Constructor<Student> constructor =
                    clazz.getDeclaredConstructor(String.class,String.class,double.class);
            Student student = constructor.newInstance("zjsd","032",99);
            //
            // 调用构造方法对象的newInstance方法创建一个实例对象
            System.out.println("通过构造方法对象获取实例变量");
            System.out.println(student);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 反射工具类-Class
     */
    public static void Test01() {
        //（1）类名.class
        Class<Student> clazz = Student.class;
        try {
        //class的forName（）静态方法
            Class clazz1 = Class.forName("com.hf.reflectiondemo.Student");
            System.out.println("clazz----clazz1:"+(clazz==clazz1));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //(3)实例对象。getClass()方法
        Student student = new Student();
        //继承Object类方法
        Class clazz2 = student.getClass();
        System.out.println("clazz-----clazz2:"+(clazz==clazz2));

        //通过class获取类的实例对象
        try {
            //等价new
            Student student1 = clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }
}
